import {session} from 'electron'
import fs from 'fs'
import path from 'path'
import installExtension, {REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS} from 'electron-devtools-installer'
const pluginNames = {
  REDUX_DEVELOPER_TOOLS: 'gpphkfbcpidddadnkolkpfckpihlkkil',
  REDUX_DEVETOOLS: 'nnkgneoiohoecpdiaponcejilbhhikei',
}
const url = path.resolve(process.env.LOCALAPPDATA, './Microsoft/Edge/User Data/Default/Extensions')
// 获取目标目录下的所有文件夹/文件
const readdir = fs.readdirSync(url, {encoding: 'utf8'})
const pluginsUrl = Object.keys(pluginNames).reduce((acc, key) => {
  if (readdir.includes(pluginNames[key])) {
    let readdirItem = fs.readdirSync(path.resolve(url, `./${pluginNames[key]}`), {encoding: 'utf8'})
    if (readdirItem.length > 0) {
      acc[key] = path.resolve(url, `./${pluginNames[key]}/${readdirItem[0]}`)
    }
  } else {
    acc[key] = ''
  }
  return acc
}, {})

// const REDUX_DEVELOPER_TOOLS = "C:\\Users\\j2494\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default\\Extensions\\gpphkfbcpidddadnkolkpfckpihlkkil\\4.21.0_0"
// const REDUX_DEVETOOLS = "C:\\Users\\j2494\\AppData\\Local\\Microsoft\\Edge\\User Data\\Default\\Extensions\\nnkgneoiohoecpdiaponcejilbhhikei\\2.17.0_0"


export default function () {
  session.defaultSession.loadExtension(pluginsUrl.REDUX_DEVELOPER_TOOLS)
  session.defaultSession.loadExtension(pluginsUrl.REDUX_DEVETOOLS)
  // installExtension(REDUX_DEVTOOLS, REACT_DEVELOPER_TOOLS)
  //   .then((name) => console.log(`Added Extension:  ${name}`))
  //   .catch((err) => console.log('An error occurred: ', err));
}
